// eslint-disable-next-line @typescript-eslint/no-var-requires
const merge = require('webpack-merge');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tsImportPluginFactory = require('ts-import-plugin');

// eslint-disable-next-line @typescript-eslint/no-var-requires
const pxtoviewport = require('postcss-px-to-viewport');

// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
const rootConfig = require('./public/config/root.config.json');
const baseUrl = rootConfig['body']['prefixes'][0]['base-url'];
const webUrl = rootConfig['body']['prefixes'][1]['web-url'];

function resolve(dir) {
  return path.join(__dirname, dir);
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const autoprefixer = require('autoprefixer');
module.exports = {
  publicPath: baseUrl,
  devServer: {
    proxy: {
      [`${webUrl}/customer`]: {
        target: 'http://10.254.10.9:31028/',
        // target: 'http://10.254.2.95:7002/mock/606',
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          [`^${webUrl}/customer`]: '/customer',
        },
      },
      [`${webUrl}/redis/v1`]: {
        target: 'http://10.254.10.9:31028/',
        // target: 'http://10.154.5.146:7002/mock/897',
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          [`^${webUrl}/redis/v1`]: '/redis/v1',
        },
      },
      [`${webUrl}/apps`]: {
        // target: 'http://10.254.10.116:31018',
        target: 'http://10.254.2.95:7002/mock/606',
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          [`^${webUrl}/apps`]: '/apps',
        },
      },
      'api/web/cmp/sop': {
        target: 'http://10.154.5.172:38088',
        // target: 'http://10.154.5.146:7002/mock/760',
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/api/web/cmp/sop': '/sop',
        },
      },
      'api/web/paas-common': {
        target: 'http://10.144.91.45:19090',
        // target: 'http://10.154.5.146:7002/mock/897',
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/api/web/paas-common': '/paas-common',
        },
      },
    },
    disableHostCheck: true,
  },

  parallel: false,
  css: {
    loaderOptions: {
      //配置less主题
      less: {
        lessOptions: {
          modifyVars: {
            // 直接覆盖变量
            'text-color': '#111',
            'border-color': '#eee',
            // 或者可以通过 less 文件覆盖（文件路径为绝对路径）
            hack: `true; @import "./src/theme/var.less";`,
          },
        },
      },
      //配置路vw vm适配
      postcss: {
        plugins: [
          autoprefixer(),
          pxtoviewport({
            viewportWidth: 375,
          }),
        ],
      },
    },
  },
  //配置路径别名
  configureWebpack: {
    resolve: {
      alias: {
        '@': resolve('src'),
        '@assets': resolve('src/assets'),
      },
    },
  },

  chainWebpack: (config) => {
    config.module
      .rule('ts')
      .use('ts-loader')
      .tap((options) => {
        options = merge(options, {
          transpileOnly: true,
          getCustomTransformers: () => ({
            before: [
              tsImportPluginFactory({
                libraryName: 'vant',
                libraryDirectory: 'es',
                style: (name) => `${name}/style/less`,
                // style: true
              }),
            ],
          }),
          compilerOptions: {
            module: 'es2015',
          },
        });
        return options;
      });
  },
};
